Git
CLI
tool
versioning
Git is a version control system mostly used for code.
Configuration
- Set username (for your commits)
-
git config --global user.name "myname"
- Set email (for your commits)
-
git config --global user.email "my@email.com"
- Enable CLI color codes
-
git config --global color.ui auto
New repositories
- Download git repo from server
-
git clone url.com
Changes
Code change workflow
- Get changes from remote repository
-
git fetch
- Check differences to branch you want merge into yours
-
git diff feature1..dev
- Merge changes from remote branch into your branch
-
git merge origin master
- Check what files are(n’t) staged for commit
-
git status
- Stage all files that are tracked (not in .gitignore)
-
git add .
- If necessary: Unstage all files (or a specific file) that are staged
-
git reset [specific-file]
- Commit all staged files (add to versioning history)
-
git commit -m "[ticket-id + what you changed]"
- Load commits into remote git repository
-
git push
Managing changes
- See changes due to last pull request
-
git log -p -2
Pull changes into dev/test/master branch
- Pull changes into dev branch (and add your changes at the end)
-
git fetch git checkout dev git pull --rebase feature1branch
- Pull only specific commits into your branch
-
git cherry-pick [commit-SHA]
Undo and Rollback
- Rollback the last commit
-
git reset head~1
To provide the files from the last commit: git reset --soft HEAD~1
- Rollback to a specific commit
-
You can still see the changes from your HEAD in your files. To make the reset materialize, you need to discard these unstaged changes in git.
git reset [commit-SHA]
Since you are now several commits behind the remote, you have to force push the changes: git push -f origin [myBranchyBranch]
.
To create a new commit to undo earlier commits, use git revert
.
- Travel back to a specific commit
-
git checkout [commit-SHA]
- Delete sensitive data from history:
- docs.github.com
Branches
- List all branches and show which one you are on
-
git branch -a
- Create new branch and check it out
-
git checkout -b [branch-name]
- Change to a specific branch
-
git checkout [specific-branch]
- Delete a specific branch
-
git branch -d [specific-branch]
- Rename the current branch
-
git branch -m myNewBranch
History
- Show commit history with branch dependencies
-
git log --graph --oneline